package com.oracle.oa.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.oracle.oa.biz.DepartInfoBiz;
import com.oracle.oa.biz.impl.DepartInfoBizImpl;
import com.oracle.oa.dao.UserInfoDao;
import com.oracle.oa.entity.Departinfo;
import com.oracle.oa.entity.Userinfo;

public class UserInfoDaoImpl extends BaseDaoImpl implements UserInfoDao {
	
	/**
	 * 根据用户部门和用户id查询
	 */
	
	public List getBydepartAndUserid(Object departname, Object userid) {
		List<Userinfo> list = null;
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Criteria c = session.createCriteria(Userinfo.class);
		c.add(Restrictions.eq("departid", departname))
		 .add(Restrictions.eq("userid", userid));
		list=c.list();
		session.close();
		return list;
	}

	/**
	 * 根据用户部门和用户名字查询
	 */
	
	public List getBydepartAndUsername(Object departname, Object username) {
		List<Userinfo> list = null;
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Criteria c = session.createCriteria(Userinfo.class);
		c.add(Restrictions.eq("departid", departname))
		 .add(Restrictions.like("username", username+"%"));
		list=c.list();
		session.close();
		return list;
	}

	/**
	 * 根据用户部门和用户id和用户名字查询
	 */
	
	public List getBydepartAndUsernameandUserid(Object departname,
			Object userid,Object username) {
		List<Userinfo> list = null;
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Criteria c = session.createCriteria(Userinfo.class);
		c.add(Restrictions.eq("departid", departname))
		 .add(Restrictions.eq("userid", userid))
		 .add(Restrictions.like("username", username+"%"));
		list=c.list();
		session.close();
		return list;
	}

	/**
	 * 根据用户id和用户名字查询
	 */
	
	public List getUseridAndUsername(Object userid, Object username) {
		List<Userinfo> list = null;
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Criteria c = session.createCriteria(Userinfo.class);
		c.add(Restrictions.eq("userid", userid))
		 .add(Restrictions.like("username", username+"%"));
		list=c.list();
		session.close();
		return list;
	}
	
	/**
	 * 根据部门id查询某部门所有员工(武明月)
	 */
	
	public List<Userinfo> getByDeptId(Long deptid) {
		// 根据部门id查询用户
		List<Userinfo> userlist=null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		DepartInfoBiz db=(DepartInfoBiz) ac.getBean("dbiz");
		Departinfo dept=(Departinfo) db.getById(Departinfo.class, deptid);
		
		Criteria cri=session.createCriteria(Userinfo.class);
		cri.add(Restrictions.eq("departid", dept.getDepartname()));
		userlist=cri.list();
		
		return userlist;
	}

	/**
	 * 根据角色id查询某职位所有员工（武明月）
	 */
	
	public List<Userinfo> getByRoleid(Long roleid) {
		// 通过角色id查询所有用户
		List<Userinfo> userlist=null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		
		Criteria c = session.createCriteria(Userinfo.class);
		c.add(Restrictions.eq("roleinfo.roleid", roleid));
		userlist = c.list();
		
		return userlist;
	}

	/**
	 * 根据用户姓名模糊查询
	 */
	
	public List<Userinfo> getByPorUsername(Object username) {
		List<Userinfo> list = null;
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Criteria c = session.createCriteria(Userinfo.class);
		c.add(Restrictions.like("username", username+"%"));
		list=c.list();
		session.close();
		return list;
	}
}
